package com.younion.business.impl;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.younion.business.IMemberCardCostCountService;
import com.younion.common.constants.CostType;
import com.younion.common.constants.TemplateTargetType;
import com.younion.common.exception.BusinessException;
import com.younion.common.paginator.Paginator;
import com.younion.dao.CostDAO;
import com.younion.dao.MemberCardCostCountDAO;
import com.younion.dao.MemberCardDAO;
import com.younion.dao.RechargeLogDAO;
import com.younion.util.YounionUtil;
import com.younion.vo.CostVO;
import com.younion.vo.CustomerVO;
import com.younion.vo.MemberCardCostCountVO;
import com.younion.vo.MemberCardVO;
import com.younion.vo.MembercardServiceVO;
import com.younion.vo.ProductVO;
import com.younion.vo.RechargeLogVO;
import com.younion.vo.UserVO;

import cpcns.component.common.base.BaseException;
import cpcns.jhsks.util.MapCollectionUtils;

 
public class MemberCardCostCountServiceImpl implements IMemberCardCostCountService {

	protected static final Log logger = LogFactory.getLog(MemberCardCostCountServiceImpl.class);

	private MemberCardCostCountDAO memberCardCostCountDAO;
	
	@Autowired
	private RechargeLogDAO rechargeLogDAO;
	@Autowired
	private CostDAO costDAO;
	
	@Autowired
	private MemberCardDAO memberCardDAO;
	
	public Collection getAll() throws Exception {
		return memberCardCostCountDAO.selectAll();
	}

	public MemberCardCostCountVO getById(String id) throws Exception {
		if (StringUtils.isEmpty(id) || Integer.parseInt(id)<1)
			return null;
		try {
			Map paramMap = new HashMap();
			paramMap.put("id", new Integer(id));
			Object obj = memberCardCostCountDAO.selectById(paramMap);
			return obj==null?null:(MemberCardCostCountVO)obj;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(MemberCardCostCountServiceImpl.class.getName()+" >> public MemberCardCostCountVO get(String id) error occur:",e);
		}
	}
	public Collection getByMap(Map paramMap) throws BaseException {
		if(CollectionUtils.isEmpty(paramMap)){
			return null;
		}
		try{
			return memberCardCostCountDAO.selectByMap(paramMap);
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			throw new BaseException(MemberCardCostCountServiceImpl.class.getName()+" >> public Collection getByMap(Map map) error occur:",e);
		}
	}

	public int getCountByMap(Map paramMap) throws BaseException {
		try {
			return memberCardCostCountDAO.selectCountByMap(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(MemberCardCostCountServiceImpl.class.getName()
					+ " >> public Collection getCountByMap(Map map) error occur:", e);
		}
	}

	public List get(Map paramMap, Paginator paginator) throws Exception {
		if (MapCollectionUtils.isEmpty(paramMap)) {
			return null;
		}
		try {
			Map map = new HashMap();
			map.put("paramMap", paramMap);
			return memberCardCostCountDAO.selectForPage(map, paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(MemberCardCostCountServiceImpl.class.getName()+" >> public Page (Map paramMap, Paginator paginator) error occur:",e);
		}
	}

	public MemberCardCostCountVO save(MemberCardCostCountVO voObject) throws Exception {
		if (voObject == null)
			return null;
		try {
			Object id = null;
			id = voObject.getId();
			Map paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(voObject));
			if (id != null) {
				memberCardCostCountDAO.updateByMap(paramMap);
				return voObject;
			} else {
				return (MemberCardCostCountVO)memberCardCostCountDAO.insertByVo(voObject);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(MemberCardCostCountServiceImpl.class.getName()+" >> public MemberCardCostCountVO save(MemberCardCostCountVO voObject) error occur:",e);
		}
	}

	public Collection save(Collection voList, List<RechargeLogVO> rechangeLogs ) throws Exception {
		if (CollectionUtils.isEmpty(voList))
			return null;
		try {
			Iterator it = voList.iterator();
			while (it.hasNext()) {
				MemberCardCostCountVO data = (MemberCardCostCountVO) it.next();
				data = save(data);
			}
			rechargeLogDAO.batchInsert(rechangeLogs);
			return voList;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(MemberCardCostCountServiceImpl.class.getName()+" >> public Collection save(Collection voList) error occur:",e);
		}
	}

	public boolean remove(List ids) throws Exception {
		if (CollectionUtils.isEmpty(ids))
			return false;
		try{
			memberCardCostCountDAO.deleteByIds(ids);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(MemberCardCostCountServiceImpl.class.getName()+" >> public boolean remove(List ids) error occur:",e);
		}
	}

	public boolean removeByMap(Map paramMap) throws Exception {
		if (CollectionUtils.isEmpty(paramMap))
			return false;
		try {
			memberCardCostCountDAO.deleteByMap(paramMap);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(MemberCardCostCountServiceImpl.class.getName()+" >> public boolean removeByMap(Map paramMap) error occur:",e);
		}
	}
	
	public boolean updateCostCount(UserVO operator, List<MemberCardCostCountVO> costCounts, 
			Integer count, Integer membercardId)  throws Exception {
		if (CollectionUtils.isEmpty(costCounts))
			return false;
		List<CostVO> costLogs = Lists.newArrayList();
		List<MemberCardCostCountVO> updateCostCounts = Lists.newArrayList();
		Map memberCardParamMap = Maps.newHashMap();
		memberCardParamMap.put("id", membercardId);
		MemberCardVO card = memberCardDAO.selectById(memberCardParamMap);
		for(MemberCardCostCountVO mc : costCounts){
			if(mc.getCount() < count){
				throw new BusinessException("您的次数不足");
			}else{
				CostVO costLog = new CostVO();
				costLog.setCardEntity(new MemberCardVO(membercardId));
				costLog.setCostDate(new Date());
				costLog.setOperator(operator);
				costLog.setProduct(new ProductVO(mc.getProduct().getId()));
				costLog.setType(CostType.TIMES.getName());
				costLog.setScore(count);
				costLog.setCustomer(new CustomerVO(card.getCustomerId()));
				mc.setCount(mc.getCount() - count);
				costLogs.add(costLog);
				updateCostCounts.add(mc);
				save(mc);
			}
			
		}
		costDAO.batchInsert(costLogs);
		return true;
	}
	public void insertVos(List<MemberCardCostCountVO> memberCardCostCountVOs) throws Exception {
		memberCardCostCountDAO.batchInsert(memberCardCostCountVOs);
	}
	public List selectByIds(List ids) throws Exception {
		return memberCardCostCountDAO.selectByIds(ids);
	}
	
	public List selectByMemberIds(List memberIds) throws Exception {
		return memberCardCostCountDAO.selectByMemberIds(memberIds);
	}
	/**
	 * @param service
	 */
	public void setMemberCardCostCountDAO(MemberCardCostCountDAO memberCardCostCountDAO) {
		this.memberCardCostCountDAO = memberCardCostCountDAO;
	}


	public RechargeLogDAO getRechargeLogDAO() {
		return rechargeLogDAO;
	}

	public void setRechargeLogDAO(RechargeLogDAO rechargeLogDAO) {
		this.rechargeLogDAO = rechargeLogDAO;
	}

	public MemberCardCostCountDAO getMemberCardCostCountDAO() {
		return memberCardCostCountDAO;
	}

	public CostDAO getCostDAO() {
		return costDAO;
	}

	public void setCostDAO(CostDAO costDAO) {
		this.costDAO = costDAO;
	}

	public MemberCardDAO getMemberCardDAO() {
		return memberCardDAO;
	}

	public void setMemberCardDAO(MemberCardDAO memberCardDAO) {
		this.memberCardDAO = memberCardDAO;
	}
	
}